package com.zhl.dao.impl;

import com.zhl.dao.UserDao;
import com.zhl.entity.User;
import com.zhl.utils.ConnUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.security.auth.login.Configuration;
import java.sql.SQLException;

@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Autowired
    private ConnUtils connUtils;
    @Autowired
    private QueryRunner qr;

    /**
     * 根据用户名查询用户
     * @param userName 返回用户信息
     * @return
     */
    @Override
    public User findUserByUserName(String userName) {
        try {
            return qr.query(connUtils.getConnection(),"select * from user where username=?",
            new BeanHandler<User>(User.class),userName);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 更新用户信息
     * @param user
     */
    @Override
    public void updateUser(User user) {
        try {
            qr.update(connUtils.getConnection(),"update user set username=?,password=?,money=? where id=?",
            user.getUsername(),user.getPassword(),user.getMoney(),user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
